package problem;

/**
 * @author RunningShrimp
 * @date 2021/5/30  15:48
 * @see <a href="https://leetcode-cn.com/problems/first-unique-character-in-a-string/">387. 字符串中的第一个唯一字符</a>
 */
public class FirstUniqueCharacterInString {
    public int firstUniqChar(String s) {
        if (s.length() == 1) {
            return 0;
        }
        int[] chars = new int[26];

        for (int i = 0; i < s.length(); i++) {
            chars[s.charAt(i) - 'a']++;
        }

        for (int i = 0; i < s.length(); i++) {
            if (chars[s.charAt(i) - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new FirstUniqueCharacterInString().firstUniqChar("leetcode"));
    }
}
